package com.xsz.controller;

import com.xsz.entity.Possition;
import com.xsz.entity.Resume;
import com.xsz.entity.User;
import com.xsz.service.ResumeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@RestController
@RequestMapping("file")
public class FileController {
		@Resource
		ResumeService resumeService;
	
	 	@Value("${fileUpLoadPath}")
	    String filePath;

	 	@PostMapping("/upload")
	   // @RequestMapping(value="/testUpload3",method=RequestMethod.POST)
	    public void testUploadFile(HttpServletRequest req,MultipartHttpServletRequest multiReq) throws IOException{
			String username=req.getParameter("username");
			System.out.println("用户名字："+username);

	 		MultipartFile multipartFile= multiReq.getFile("file");
	        String filename=multipartFile.getOriginalFilename();
	        System.out.println("文件名字："+multipartFile.getOriginalFilename());
	        File file=new File(filePath+username+"的"+filename);
	        multipartFile.transferTo(file);

			String pid=req.getParameter("fileuploadid");
			System.out.println("职位id："+pid);

			Possition possition =new Possition();
			possition.setId(pid);
			Set<Possition> possitionList=new HashSet<>();
			possitionList.add(possition);

			User user = (User) SecurityUtils.getSubject().getPrincipal();

			Resume resume=new Resume();
			resume.setCreateBy(user.getId());
			resume.setCreateDate(new Date());
			resume.setName(username+"的简历");
			resume.setAddress(filePath+filename);
			resume.setPossitionList(possitionList);

	        resumeService.add(resume);
	    }

}